define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Searchorder, $window, $timeout, GoPage) {
		$scope.datas = new Object();
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.input.key = '';
		$scope.order = 1;
		$scope.isLoad = false; //可以加载
		//最新排序
		$scope.order = 1; //默认
		$scope.count = null;
		//画面加载
		$scope.$on('$ionicView.afterEnter', function(){
				var lan = 0; //这是状态栏，只有ios的APP才有
		var tab = 0;
		var bars = document.getElementById("nav-bar-title");
		var bar = ((bars != undefined) ? 44 : 0);
		if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { lan = 20; }
		//整体高度
		var all = $window.innerHeight;
		var search_top1 = document.getElementById("search-top1").clientHeight;
		var search_top2 = document.getElementById("search-top2").clientHeight;
		$scope.search_myObj = { //滚动条可视范围
			"height": all - lan - bar - tab - search_top1 - search_top2 + "px"
		};
			},false)
		//动态设置高度
		
		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Searchorder.get_list($scope.input.key, 1, $scope.order, function(data) {
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;

			}, function() {
				$timeout(function() {
					$scope.$broadcast('scroll.refreshComplete');
					$scope.isLoad = false;
				}, 1500);
			})

		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.datas.findgoods == undefined)
				$scope.input.nPage = 0;
			else
			if($scope.datas.page.count <= $scope.datas.findgoods.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.datas.page.count<$scope.input.nPage*10){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *10;
				}else{
					$scope.count = null;
				}
			}
			Searchorder.get_list($scope.input.key, $scope.input.nPage + 1, $scope.order, function(data) {
				if(data.findgoods.length > 0) {
					if($scope.datas.findgoods != undefined) {
						if($scope.count!=null)
						$scope.datas.findgoods.splice($scope.count,$scope.datas.page.count-$scope.count);
						$scope.datas.findgoods = $scope.datas.findgoods.concat(data.findgoods);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}
			}, function() {
				$timeout(function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.isLoad = false;
				}, 1500);
			})

		};

		//查询

		$scope.doSearch = function(order) {
			$scope.order = 1;
			Searchorder.get_list($scope.input.key, 1, $scope.order, function(data) {
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;

			})
		};
		
		//最新排序/最热排序，只要最新和最热
		$scope.orderGoods = function(order) {
			if($scope.order==order){//防止重复请求
				return;	
			}else{
				$scope.order = order;
			}
			Searchorder.get_list($scope.input.key, 1, $scope.order, function(data) {
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			})
		}

		//页面跳转
		$scope.onshow_myproductdetail = function(id, region, store_name, store_id) {
			GoPage.go_states("myproductdetail", {
				'detail_id': id
			});
		}
		//加载数据
		$scope.init_data = function() {
			Searchorder.get_list($scope.input.key, 1, $scope.order, function(data) {
				if(data != null)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			})
		}
		$scope.init_data();
	}

	controller.$inject = ['$scope', 'Searchorder', '$window', '$timeout', 'GoPage'];

	return app.registerController("searchorderctrl",controller);
});